﻿#include "login.h"
#include "ui_login.h"
#include "mainwindow.h"
#include <QVariantList>

#include <qmessagebox.h>
#include "mydatabase.h"

int login::login_admin = 0;

MyDatabase *back_workerDataBase; //员工数据库
MyDatabase *back_roomClass; //员工数据库
MyDatabase *back_roomInfo; //员工数据库
MyDatabase *back_hotelBill; //员工数据库

login::login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);
//    ui->index_login->setStyleSheet("background-color: #f1f0ed;"
//                                "color:#66a9c9;");
    ui->index_login->setShortcut(Qt::Key_Return);
    ui->index_login->setShortcut(Qt::Key_Enter);

    //初始化员工数据库
    table_Init();
}

login::~login()
{
    delete ui;
    back_workerDataBase->db.close();
}

void login::on_index_login_clicked()
{
    //获取登录账号密码
    QString account = ui->index_account->text();
    QString password = ui->index_password->text();

    //是否是特殊账号
    if(account == "admin" && password == "123456")  //超级管理员权限
    {
        this->close();
        //打开主窗口
        MainWindow *m = new MainWindow(account);
        m->show();
    }
    else if(account == "datainit" && password == "")  //初始化数据库
    {
        date_Init();
        QMessageBox::information(this, "login", "数据库初始化成功！");
    }
    else{
        //查询登录账号密码是否存在
        QString sql = QString("phoneNumber = '%1' AND password = %2").arg(account, password);
        QVariantList userDatas = back_workerDataBase->Find(sql);
        //判断登录账号密码是否匹配在数据库
        bool a = userDatas.value(0) == "行号：0";

        if (a) {
            this->close();
            //打开主窗口
            MainWindow *m = new MainWindow(account);
            m->show();
        } else {
            QMessageBox::critical(this, "login", "账号不存在或者密码错误");
        }
    }
}

void login::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,width(),height(),QPixmap(":/new/images/images/login.jpg"));
}

int login::getLogin_admin()
{
    return login_admin;
}

void login::table_Init()
{
    back_workerDataBase = new MyDatabase("back","workerDataBase"); //员工数据库
    back_workerDataBase->InitDatabase("create table workerDataBase(phoneNumber char(11) PRIMARY KEY , password char(20) not null , name char(10) not null , adminID char(18) not null , lv int not null)");
}

//表内数据初始化
void login::date_Init(){
    //连接数据库
    back_roomClass = new MyDatabase("back","roomClass");  //房间类型
    back_roomClass->InitDatabase("create table roomClass(class char(10) PRIMARY KEY , image char(500), brief char(300) , numbers char(300) not null , price int not null)");
    back_roomInfo = new MyDatabase("back","roomInfo");   //订房情况
    back_roomInfo->InitDatabase("create table roomInfo(class char(10) not null , number char(10) not null , startDate date not null , endDate date not null , name char(10) not null , ID char(18) not null , phoneNumber char(11) not null , checkInDate daretime not null)");
    back_hotelBill = new MyDatabase("back","hotelBill");    //酒店经营情况
    back_hotelBill->InitDatabase("create table hotelBill(date datetime not null , class char(10) not null , number char(10) not null , collectMoney int not null , collectedMoney int not null , name char(10) not null)");

    //初始化数据
    back_workerDataBase->Add("'root' , '123456' , '小红' , '410721209902301000' , 30");
    back_workerDataBase->Add("'user' , '123456' , '小李' , '41072120991332999x' , 40");
    back_roomClass->Add("'大床房' , ':/new/images/images/zb.jpg', '这个房间很大' , '100,101,102,103' , 100");
    back_roomClass->Add("'中床房' , ':/new/images/images/zq.jpg', '这个房间很中' , '200,201,202,203' , 80");
    back_roomClass->Add("'小床房' , ':/new/images/images/gg.jpg', '这个房间很小' , '300,301,302,303' , 50");
    back_roomInfo->Add("'大床房' , '102', '2024-05-10' , '2024-05-13' , '张三' , '41074120990001142x' , '11211112222' , '2024-05-09 20:41:01' ");
    back_roomInfo->Add("'中床房' , '201', '2024-05-10' , '2024-05-11' , '李四' , '510741209900011421' , '13833338888' , '2024-05-09 20:41:01' ");
    back_roomInfo->Add("'小床房' , '303', '2024-05-10' , '2024-05-11' , '王五' , '610741209900011421' , '11011011011' , '2024-05-10 20:41:02' ");
    back_hotelBill->Add(" '2024-05-09 20:41:01' , '大床房' , '102' , '90' , '100' , '小李' ");
    back_hotelBill->Add(" '2024-05-09 20:41:01' , '中床房' , '201' , '85' , '80' , '小李' ");
    back_hotelBill->Add(" '2024-05-10 20:41:02' , '小床房' , '303' , '50' , '50' , '小李' ");
}

void login::on_dataBase_btn_clicked()
{
    date_Init();
}
